﻿using Coater.Domain;
using Coater.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coater
{
    public sealed class DeviceContext
    {
        /// <summary>
        /// DeviceContext单例对象
        /// </summary>
        public static DeviceContext Instance = new();

        public Configuration Config { get; set; }

        /// <summary>
        /// 喷嘴信息
        /// </summary>
        public List<NozzleInfo> Nozzles { get; set; }

        private DeviceContext()
        {
            LoadConfig();
            LoadNozzleConfig();
        }

        private List<NozzleInfo> GetDefaultNozzles()
        {
            List<NozzleInfo> li = new List<NozzleInfo>();
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle1 });
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle2 });
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle3 });
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle4 });
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle5 });
            li.Add(new NozzleInfo() { Nozzle = Enum.NozzleEnum.Nozzle6 });
            return li;
        }

        public void LoadNozzleConfig()
        {
            Nozzles = XMLHelper.GetSettings<List<NozzleInfo>>("NozzleInfo");
            if (Nozzles.Count != 6)
            {
                Nozzles = GetDefaultNozzles();
                SaveNozzleConfig(Nozzles);
            }
        }

        public void SaveNozzleConfig(List<NozzleInfo> nozzles)
        {
            XMLHelper.SaveSettings("NozzleInfo", nozzles);
        }

        public void LoadConfig()
        {
            Config = XMLHelper.GetSettings<Configuration>("Configuration");
        }

        public void SaveConfig(Configuration config = null)
        {
            XMLHelper.SaveSettings("Configuration", config == null ? Config : config);
        }

    }
}
